package cn.ffcs.uec.salary.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.salary.action.bean.UecSalaryPiEditBean;
import cn.ffcs.uec.salary.constant.EnumUecSalary;
import cn.ffcs.uec.salary.manager.UecSalaryPayitemManager;
import cn.ffcs.uec.salary.model.UecSalaryPayitem;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;

@Controller
@Scope("prototype")
public class UecSalaryPiEditComposer extends BasePortletComposer {
	private static final long serialVersionUID = -6096343177536382090L;

	private UecSalaryPiEditBean bean = new UecSalaryPiEditBean();

	private String opType;

	@Resource
	private UecSalaryPayitemManager uecSalaryPayitemManager;

	UecSalaryPayitem uecSalaryPayitem;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * window初始化.
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onCreate$uecSalaryPiEditWin() throws Exception {
		bindBean();
	}

	/**
	 * 页面初始化
	 * 
	 * @throws Exception
	 */
	public void bindBean() throws Exception {
		opType = StrUtil.strnull(arg.get("opType"));
		if (EnumUecSalary.ADD.getValue().equals(opType)) {
			bean.getUecSalaryPiEditWin().setTitle("薪资发放项目事实新增");
		} else if (EnumUecSalary.MOD.getValue().equals(opType)) {
			bean.getUecSalaryPiEditWin().setTitle("薪资发放项目事实修改");
			initPageBean();
		} else {
			bean.getUecSalaryPiEditWin().setTitle("薪资发放项目事实查看");
			bean.getBtnToolBar().setVisible(false);
			PubUtil.disableComponent(bean.getUecSalaryPiDiv(), true);
			bean.getCyear().setDisabled(true);
			bean.getCreationtime().setDisabled(true);
			initPageBean();
		}
	}

	public void initPageBean() throws Exception {
		uecSalaryPayitem = (UecSalaryPayitem) arg.get("uecSalaryPayitem");
		PubUtil.fillBeanFromPo(uecSalaryPayitem, bean);

		String cyearStr = uecSalaryPayitem.getCyear();

		if (!StrUtil.isNullOrEmpty(cyearStr)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			Date date = null;
			try {
				date = sdf.parse(cyearStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}

			bean.getCyear().setValue(date);
		}

		String crttimeStr = uecSalaryPayitem.getCreationtime();

		if (!StrUtil.isNullOrEmpty(crttimeStr)) {
			SimpleDateFormat sdfCrt = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			Date crttimeDate = null;
			try {
				crttimeDate = sdfCrt.parse(crttimeStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}

			bean.getCreationtime().setValue(crttimeDate);
		}
	}

	public void initPoModel() {
		PubUtil.fillPoFromBean(bean, uecSalaryPayitem);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		String cyearStr = sdf.format(bean.getCyear().getValue());
		uecSalaryPayitem.setCyear(cyearStr);

		SimpleDateFormat sdfCrt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String creationtimeStr = sdfCrt.format(bean.getCreationtime()
				.getValue());
		uecSalaryPayitem.setCreationtime(creationtimeStr);
	}

	/**
	 * 保存.
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = checkUacStaffData();
		if (msg != null) {
			Messagebox.show(msg);
			return;
		}

		if (EnumUecSalary.ADD.getValue().equals(opType)) {
			uecSalaryPayitem = UecSalaryPayitem.newInstance();
			initPoModel();
			uecSalaryPayitem.addOnly();
			Events.postEvent(Events.ON_OK, bean.getUecSalaryPiEditWin(),
					uecSalaryPayitem);
		} else if (EnumUecSalary.MOD.getValue().equals(opType)) {
			initPoModel();
			uecSalaryPayitem.updateOnly();
			Events.postEvent(Events.ON_OK, bean.getUecSalaryPiEditWin(),
					uecSalaryPayitem);
		}

		onCancel();
	}

	/**
	 * 取消.
	 */
	public void onCancel() {
		bean.getUecSalaryPiEditWin().onClose();
	}

	private String checkUacStaffData() {
		// BaseInfo Check
		if (StrUtil.isNullOrEmpty(bean.getCyear().getValue())) {
			return "请选择薪资年度";
		} else if (StrUtil.isNullOrEmpty(bean.getBankaccount().getValue())) {
			return "请填写银行账号";
		} else if (StrUtil.checkBlank(bean.getBankaccount().getValue())) {
			return "银行账号中有空格";
		} else if (StrUtil.isNullOrEmpty(bean.getCategoryId().getValue())) {
			return "请填写项目分类";
		} else if (StrUtil.checkBlank(bean.getCategoryId().getValue())) {
			return "项目分类中有空格";
		} else if (StrUtil.isNullOrEmpty(bean.getName().getValue())) {
			return "请填写发放项目名称";
		} else if (StrUtil.checkBlank(bean.getName().getValue())) {
			return "发放项目名称中有空格";
		} else if (!StrUtil.checkBlank(bean.getCperiod().getValue())
				&& bean.getCperiod().getValue().length() > 2) {
			return "填写薪资期间长度超过2位数上限";
		} else {
			return null;
		}
	}
}
